<script setup>
import { ref, reactive, toRaw } from 'vue'
import { service, cache } from '@utils'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refresh'])

const props = defineProps({
    depts: Array,
    types: Array
})

const visible = ref(false)
const formRef = ref()
const parents = ref()
const disable = ref(false)

const state = reactive({
    param: {}
})

const rules = {
    parentId: [{ required: true, message: '请选择', trigger: 'change' }],
    label: [{ required: true, message: '请输入', trigger: 'blur' }],
    menuType: [{ required: true, message: '请选择', trigger: 'change' }],
    perms: [{ required: true, message: '请输入', trigger: 'blur' }],
    display: [{ required: true, message: '请选择', trigger: 'change' }],
    path: [{ required: true, message: '请输入', trigger: 'blur' }],
    shareable: [{ required: true, message: '请选择', trigger: 'change' }]
}

const parentNo = [{id: -1, label: '部门'}]

const open = (value) => {
    if (value) {
        if (value.parentId === -1) {
            parents.value = parentNo
        } else {
            parents.value = props.depts.filter((item) => item.id === value.parentId)
        }
        disable.value = true
    } else {
        getDepts()
        disable.value = false
    }
    state.param = { ...value }
    state.param.sort = state.param.sort || 0
    visible.value = true
}

const close = () => {
    visible.value = false
    state.param = {}
    parents.value = null
    disable.value = false
}

const getDepts = async () => {
    const res = await service.auth.deptTree()
    console.log('=sdf=ds', res)
    parents.value = parentNo.concat(res)
}

const sure = async () => {
    formRef.value.validate((valid) => {
        if (valid) {
            realSubmit()
        }
    })
}
const realSubmit = async () => {
    const res = await service.auth.deptEdit(toRaw(state.param))
    if (res) {
        ElMessage.success('修改成功')
        emit('refresh', {...toRaw(state.param), ...res, isUpdate: !!state.param.id})
        close()
    }
}
//把方法暴露给父组件
defineExpose({
    open
})
</script>
    
    <template>
    <el-dialog v-model="visible" :title="!!state.param.id ? '修改' : '新增'" center :close-on-click-modal="false" @close="close"
        destroy-on-close :width="500">
        <section>
            <el-form :model="state.param" :rules="rules" ref="formRef" label-width="100px" label-suffix=":" onsubmit="return false;">
                <el-form-item label="上级部门" prop="parentId">
                    <el-tree-select v-model="state.param.parentId" :data="parents" :render-after-expand="false" check-strictly clearable
                        :props="{value: 'id', label: 'label'}" style="width: 100%" :disabled="disable" />
                </el-form-item>
                <el-form-item label="部门名称" prop="label">
                    <el-input v-model="state.param.label" placeholder="请输入菜单名称" clearable></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input type="number" v-model="state.param.sort" placeholder="请输入排序"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="content">
                    <el-input v-model="state.param.content" placeholder="请输入描述"></el-input>
                </el-form-item>
            </el-form>
        </section>

        <template #footer>
            <span class="flex-align-end">
                <div class="margin-right20">
                    <el-button @click="close">取消</el-button>
                </div>
                <el-button type="primary" @click="sure">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
    